<?php
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: ?home'); }
unset($sql);

// Kaires puses Aukstyn paneles stumdymas
if (isset($_POST['panel_up_l']) && $_POST['panel_up_l'] == 'Aukštyn') {
	//Keiciamos paneles ID ir vieta, bei vieta kur turi buti	$busima = mysql_fetch_assoc(mysql_query1("SELECT `place` FROM `panel` WHERE `align`='L' AND `id`=" . escape($_POST['L_panel']) ." AND `show`='Y'"));
	$id = $_POST['L_panel'];
	$vieta = $busima['place'];
	$busima_vieta = $vieta - 1;
    echo $busima_vieta;;
	// Paneles ID, vieta, busima vieta kur norima istumti is jos vietos
	$keiciama_p = mysql_fetch_assoc(mysql_query1("SELECT `id`,place FROM panel WHERE `align`='L' AND `place`='" . $busima_vieta ."' AND `show`='Y'"));
	$id2 = $keiciama_p['id'];
	$vieta2 = $keiciama_p['place'];
	$busima_vieta2 = $vieta2 + 1;

	// Patikrinama ar norima panele perkelti aukstyn nera 1 vietoje
	if ($vieta != 1) {		// Sukeiciam panelem
		// Pirma pakeiciam norima panele i jai norima vieta
		mysql_query1("UPDATE `panel` SET `place`='" . $busima_vieta ."' WHERE `id`='" . $id ."'");
		// Pakeiciam istumta panele zemyn
		mysql_query1("UPDATE `panel` SET  `place`='" . $busima_vieta2 ."' WHERE `id`='" . $id2 ."'");
	}
	unset($busima,$id,$vieta,$busima_vieta,$keiciama_p,$id2,$busima_vieta2,$vieta2);
	header("Location: ?id,46;a,9");
}
// Kaires puses Zemyn paneles stumdymas
if (isset($_POST['panel_down_l']) && $_POST['panel_down_l'] == 'Žemyn') {
	//Keiciamos paneles ID ir vieta, bei vieta kur turi buti
	$busima = mysql_fetch_assoc(mysql_query1("SELECT `place` FROM `panel` WHERE `align`='L' AND `id`=" . escape($_POST['L_panel']) ." AND `show`='Y'"));
	$id = $_POST['L_panel'];
	$vieta = $busima['place'];
	$busima_vieta = $vieta + 1;
	// Paneles ID, vieta, busima vieta kur norima istumti is jos vietos
	$keiciama_p = mysql_fetch_assoc(mysql_query1("SELECT `id`,place FROM panel WHERE `align`='L' AND `place`='" . $busima_vieta ."' AND `show`='Y'"));
	$id2 = $keiciama_p['id'];
	$vieta2 = $keiciama_p['place'];
	$busima_vieta2 = $vieta2 - 1;

	// Patikrinama ar norima panele perkelti zemyn nera paskutinej ir tai vietoj
	$sql = mysql_num_rows(mysql_query1("SELECT id FROM `panel` WHERE `align`='L' AND `show`='Y'"));
	if ($busima_vieta <= $sql) {
		// Sukeiciam panelem
		// Pirma pakeiciam norima panele i jai norima vieta
		mysql_query1("UPDATE `panel` SET `place`='" . $busima_vieta ."' WHERE `id`='" . $id ."'");
		// Pakeiciam istumta panele zemyn
		mysql_query1("UPDATE `panel` SET  `place`='" . $busima_vieta2 ."' WHERE `id`='" . $id2 ."'");
	}
	unset($busima,$id,$vieta,$busima_vieta,$keiciama_p,$id2,$busima_vieta2,$vieta2,$sql);
	header("Location: ?id,46;a,9");
}
// Kaires puses paneles isungimas
if (isset($_POST['panel_off_l']) && $_POST['panel_off_l'] == 'Išjungti') {	$count = mysql_num_rows(mysql_query1("SELECT `id` FROM `panel` WHERE `align`='L' AND `show`='N'"));
	$count = $count + 1;	mysql_query1("UPDATE `panel` SET `show`='N', `place`='" . $count ."' WHERE `id`=" . escape($_POST['L_panel']) ."");
	unset($count);
	header("Location: ?id,46;a,9");
}
//Kaire puses paneles ijungimas
if (isset($_POST['panel_on_l']) && $_POST['panel_on_l'] == 'Įjungti') {	$count = mysql_num_rows(mysql_query1("SELECT `id` FROM `panel` WHERE `align`='L' AND `show`='Y'"));
	$total = $count + 1;
	mysql_query1("UPDATE `panel` SET `show`='Y', `place`='" . $count ."' WHERE `id`=" . escape($_POST['L_panel']) ."");
	unset($count);
	header("Location: ?id,46;a,9");
}


//Desines puses Aukstyn paneles stumdymas
if (isset($_POST['panel_up_r']) && $_POST['panel_up_r'] == 'Aukštyn') {

	//Keiciamos paneles ID ir vieta, bei vieta kur turi buti
	$busima = mysql_fetch_assoc(mysql_query1("SELECT `place` FROM `panel` WHERE `align`='R' AND `id`=" . escape($_POST['R_panel']) ." AND `show`='Y'"));
	$id = $_POST['R_panel'];
	$vieta = $busima['place'];
	$busima_vieta = $vieta - 1;
    echo $busima_vieta;;
	// Paneles ID, vieta, busima vieta kur norima istumti is jos vietos
	$keiciama_p = mysql_fetch_assoc(mysql_query1("SELECT `id`,place FROM panel WHERE `align`='R' AND `place`='" . $busima_vieta ."' AND `show`='Y'"));
	$id2 = $keiciama_p['id'];
	$vieta2 = $keiciama_p['place'];
	$busima_vieta2 = $vieta2 + 1;

	// Patikrinama ar norima panele perkelti aukstyn nera 1 vietoje
	if ($vieta != 1) {
		// Sukeiciam panelem
		// Pirma pakeiciam norima panele i jai norima vieta
		mysql_query1("UPDATE `panel` SET `place`='" . $busima_vieta ."' WHERE `id`='" . $id ."'");
		// Pakeiciam istumta panele zemyn
		mysql_query1("UPDATE `panel` SET  `place`='" . $busima_vieta2 ."' WHERE `id`='" . $id2 ."'");
	}
	unset($busima,$id,$vieta,$busima_vieta,$keiciama_p,$id2,$busima_vieta2,$vieta2);
	header("Location: ?id,46;a,9");
}
// Desines puses Zemyn paneles stumdymas
if (isset($_POST['panel_down_r']) && $_POST['panel_down_r'] == 'Žemyn') {
	//Keiciamos paneles ID ir vieta, bei vieta kur turi buti
	$busima = mysql_fetch_assoc(mysql_query1("SELECT `place` FROM `panel` WHERE `align`='R' AND `id`=" . escape($_POST['R_panel']) ." AND `show`='Y'"));
	$id = $_POST['R_panel'];
	$vieta = $busima['place'];
	$busima_vieta = $vieta + 1;
	// Paneles ID, vieta, busima vieta kur norima istumti is jos vietos
	$keiciama_p = mysql_fetch_assoc(mysql_query1("SELECT `id`,place FROM panel WHERE `align`='R' AND `place`='" . $busima_vieta ."' AND `show`='Y'"));
	$id2 = $keiciama_p['id'];
	$vieta2 = $keiciama_p['place'];
	$busima_vieta2 = $vieta2 - 1;

	// Patikrinama ar norima panele perkelti zemyn nera paskutinej ir tai vietoj
	$sql = mysql_num_rows(mysql_query1("SELECT id FROM `panel` WHERE `align`='R' AND `show`='Y'"));
	if ($busima_vieta <= $sql) {
		// Sukeiciam panelem
		// Pirma pakeiciam norima panele i jai norima vieta
		mysql_query1("UPDATE `panel` SET `place`='" . $busima_vieta ."' WHERE `id`='" . $id ."'");
		// Pakeiciam istumta panele zemyn
		mysql_query1("UPDATE `panel` SET  `place`='" . $busima_vieta2 ."' WHERE `id`='" . $id2 ."'");
	}
	unset($busima,$id,$vieta,$busima_vieta,$keiciama_p,$id2,$busima_vieta2,$vieta2,$sql);
	header("Location: ?id,46;a,9");
}
// Desines puses paneles isungimas
if (isset($_POST['panel_off_r']) && $_POST['panel_off_r'] == 'Išjungti') {
	$count = mysql_num_rows(mysql_query1("SELECT `id` FROM `panel` WHERE `align`='L' AND `show`='N'"));
	$count = $count + 1;
	mysql_query1("UPDATE `panel` SET `show`='N', `place`='" . $count ."' WHERE `id`=" . escape($_POST['R_panel']) ."");
	unset($count);
	header("Location: ?id,46;a,9");
}
//Desines puses paneles ijungimas
if (isset($_POST['panel_on_r']) && $_POST['panel_on_r'] == 'Įjungti') {
	$count = mysql_num_rows(mysql_query1("SELECT `id` FROM `panel` WHERE `align`='R' AND `show`='Y'"));
	$total = $count + 1;
	mysql_query1("UPDATE `panel` SET `show`='Y', `place`='" . $count ."' WHERE `id`=" . escape($_POST['R_panel']) ."");
	unset($count);
	header("Location: ?id,46;a,9");
}

$text = "
	<form name=\"panel\" action=\"\" method=\"post\">
		<table border=0 width=100%>
			<tr>
				<td width=20%>
					<select size=\"15\" name=\"L_panel\">
						<option value=0 disabled>___________________</option>";
					$sql = mysql_query1("SELECT * FROM panel WHERE `align`='L' AND `show`='Y' ORDER BY `place`");
					while ($row = mysql_fetch_assoc($sql)) {						$text .= "<option value='" . $row['id'] ."'>" . $row['panel'] ."</option>";
					}
					$text .= "<option value=0 disabled>___________________</option>";
					$sql = mysql_query1("SELECT * FROM panel WHERE `align`='L' AND `show`='N' ORDER BY `place`");
					while ($row = mysql_fetch_assoc($sql)) {
						$text .= "<option value='" . $row['id'] ."'>" . $row['panel'] ."</option>";
					}
					$text .= "
					</select>
				</td>
				<td valign=top width=1%>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_up_l\" type=\"submit\" value=\"Aukštyn\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_down_l\" type=\"submit\" value=\"Žemyn\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_off_l\" type=\"submit\" value=\"Išjungti\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_on_l\" type=\"submit\" value=\"Įjungti\">
				</td>
				<td valign=center align=center width=100%>
					<input type=\"submit\" value=\">>\"><br><br>
					<input type=\"submit\" value=\"<<\">
				</td>
				<td>
					<select size=\"15\" name=\"R_panel\">
						<option value=0 disabled>___________________</option>";
					$sql = mysql_query1("SELECT * FROM panel WHERE `align`='R' AND `show`='Y' ORDER BY `place`");
					while ($row = mysql_fetch_assoc($sql)) {
						$text .= "<option value=" . $row['id'] .">" . $row['panel'] ."</option>";
					}
					$text .= "<option value=0 disabled>___________________</option>";
					unset($sql);
					$sql = mysql_query1("SELECT * FROM panel WHERE `align`='R' AND `show`='N' ORDER BY `place`");
					while ($row = mysql_fetch_assoc($sql)) {
						$text .= "<option value=" . $row['id'] .">" . $row['panel'] ."</option>";
					}
					$text .= "
					</select>
				</td>
				<td valign=top width=1%>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_up_r\" type=\"submit\" value=\"Aukštyn\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_down_r\" type=\"submit\" value=\"Žemyn\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_off_r\" type=\"submit\" value=\"Išjungti\"><br><br>
					<input style=\"font-weight: bold; width: 60px;\" name=\"panel_on_r\" type=\"submit\" value=\"Įjungti\">
				</td>
			</tr>
		</table>
	</form>
";
lentele("Panelės perkėlimas", $text);
unset($text,$sql,$row);

?>